package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.BuildingDto;
import primetest.faces.BuildingListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.BuildingService;

@Named("buildingListControler")
@SessionScoped
public class BuildingListControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -2341239310873546662L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Inject
	private SessionManager sessionManager;

	private BuildingListModel buildingList = null;
	private List<BuildingDto> baseBuildingList;
	private BuildingDto selectedBuilding = new BuildingDto();
	private BuildingDto backBuilding = new BuildingDto();
	private BuildingDto newBuilding = new BuildingDto();
	private int first;
	private boolean editMode = false;
	private boolean addMode = false;

	private List<BuildingDto> filteredTerms;

	public boolean getEditMode() {
		return editMode;
	}

	public boolean getAddMode() {
		return addMode;
	}

	@Inject
	private BuildingService buildingService;

	public List<BuildingDto> getBaseBuildingList() {
		if (baseBuildingList == null) {
			try {
				baseBuildingList = buildingService.getBuildingList();
				buildingList = new BuildingListModel(baseBuildingList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseBuildingList;
	}

	public BuildingListModel getBuildingList() {
		if (baseBuildingList == null) {
			try {
				baseBuildingList = buildingService.getBuildingList();
				buildingList = new BuildingListModel(baseBuildingList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseBuildingList == null ? new BuildingListModel(
				new ArrayList<BuildingDto>()) : buildingList;
	}

	public void deleteBuilding() throws IOException {
		buildingService.deleteBuilding(selectedBuilding.getKey());
	}

	public void reload() {
		baseBuildingList = null;
	}

	public BuildingDto getSelectedBuilding() {
		return selectedBuilding;
	}

	public void setSelectedBuilding(BuildingDto selectedBuilding) {
		this.selectedBuilding = selectedBuilding;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public BuildingDto getNewBuilding() {
		return newBuilding;
	}

	public void setNewBuilding(BuildingDto newBuilding) {
		this.newBuilding = newBuilding;
	}

	public void startEditBuilding() {
		editMode = true;
		backBuilding.setAll(selectedBuilding);
	}

	public void saveEditBuilding() {
		try {
			if (selectedBuilding.isValid()) {
				buildingService.updateBuilding(selectedBuilding);
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("Floors invalid", null);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void cancelEditBuilding() {
		editMode = false;
		selectedBuilding.setAll(backBuilding);
	}

	public void startAddBuilding() {
		addMode = true;
		newBuilding = new BuildingDto();
	}

	public void addNewBuilding() throws IOException {
		if (newBuilding.isValid()) {
			buildingService.addBuilding(newBuilding);
			addMode = false;
		} else {
			sessionManager.addGlobalMessageFatal("Floors invalid", null);
		}

	}

	public void cancelAddBuilding() {
		addMode = false;
		newBuilding = new BuildingDto();
	}

	public List<BuildingDto> getFilteredTerms() {
		return filteredTerms;
	}

	public void setFilteredTerms(List<BuildingDto> filteredTerms) {
		this.filteredTerms = filteredTerms;
	}
}
